package com.ruanxie.service.impl;

import com.ruanxie.constant.JwtClaimsConstant;
import com.ruanxie.entity.User;
import com.ruanxie.mapper.UserMapper;
import com.ruanxie.properties.JwtProperties;
import com.ruanxie.service.OtherService;
import com.ruanxie.utils.JwtUtil;
import com.ruanxie.vo.user.UserLoginVO;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OtherServiceImpl implements OtherService {

    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private UserMapper userMapper;


    /**
     * 检验token
     * @param token
     * @return
     */
    @Override
    public UserLoginVO getUserByToken(String token) {
        Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(),token);
        String role=String.valueOf(claims.get(JwtClaimsConstant.role));
        String accountId=String.valueOf(claims.get(JwtClaimsConstant.accountId));
        User user=userMapper.get(new User().builder().accountId(accountId).role(role).build());
        UserLoginVO userLoginVO=new UserLoginVO();
        BeanUtils.copyProperties(user,userLoginVO);
        return userLoginVO;
    }
}
